package com.example.wanxiang.controller;

import com.example.wanxiang.dto.ChatDto;
import com.example.wanxiang.dto.ChatReturn;
import com.example.wanxiang.entity.Chat;
import com.example.wanxiang.service.ChatService;
import jakarta.annotation.Resource;

import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/chat")
public class ChatController {

    @Resource
    private ChatService chatService;

    @GetMapping("/selectPage")
    public ChatReturn getChats(@RequestBody ChatDto chatDto) {
        ChatReturn chatReturn = new ChatReturn();
        //这里改成了个根据用户id查询
        chatReturn.setList(chatService.getChatBySponsor(chatDto));
        chatReturn.setCount(chatService.getChatCount(chatDto));
        chatReturn.setPageNow(chatDto.getPage());
        chatReturn.setPageSize(chatDto.getSize());
        return chatReturn;
    }

    @PostMapping("/createChat")
    public Chat save(@RequestBody ChatDto chatDto) {
        Chat chat = new Chat();
        chat.setRead(false);
        chat.setSponsor(chatDto.getSponsor());
        chat.setRecipients(chatDto.getRecipients());
        chat.setStatus(true);
        return chatService.createChat(chat);
    }

    @DeleteMapping("/{sponsor}")
    public void delete(@PathVariable("sponsor") String sponsor) {
        chatService.deleteChat(sponsor);
    }

}
